package cn.vendai.com.service.impl;

import cn.vendai.com.entity.Conversation;
import cn.vendai.com.repository.ConversationRepository;
import cn.vendai.com.service.ConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.util.UUID;


@Service
public class ConversationServiceImpl implements ConversationService {

    @Autowired
    private ConversationRepository conversationRepository;

    /**
     * 创建会话
     * @param userId 用户ID
     * @return 会话ID
     */
    @Override
    public String createConversation(int userId) {
        // 生成 yyyy-MM-dd-userId-uuid 格式的 conversationId
        String datePrefix = LocalDate.now().toString(); // 2025-08-31
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String conversationId = datePrefix + "-" + userId + "-" + uuid;

        // 保存会话信息
        Conversation conversation = new Conversation();
        conversation.setId(conversationId);
        conversation.setUserId(String.valueOf(userId));
        conversation.setCreateTime(Instant.now());
        conversationRepository.save(conversation);

        return conversationId;
    }
}
